/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 28.07.2008
*/

package it.cums.ui.wizards;

import it.cums.core.model.Invoice;
import it.cums.core.util.Converter;
import it.cums.ui.localization.Messages;

import java.util.Date;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class InvoiceWizardPage extends WizardPage implements IWizardPage {
	private Text dueDateText;
	private Text amountText;
	private Combo conditionCombo;
	private Combo typeCombo;
	private Combo statusCombo;
	private Text paymentDateText;
	private Invoice invoice;
	private Composite container;
	
	public InvoiceWizardPage() {
		super(Messages.InvoiceWizardPage_0);
		setTitle(Messages.InvoiceWizardPage_1);
		setDescription(Messages.InvoiceWizardPage_2);
	}
	
	public InvoiceWizardPage(Invoice invoice) {
		super(Messages.InvoiceWizardPage_3);
		setTitle(Messages.InvoiceWizardPage_4);
		setDescription(Messages.InvoiceWizardPage_5);
		this.invoice = invoice;
	}

	
	public void createControl(Composite parent) {
		container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		
		Label label1 = new Label(container, SWT.NULL);
		label1.setText(Messages.InvoiceWizardPage_6);
		dueDateText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		dueDateText.setLayoutData(gd);
		dueDateText.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label3 = new Label(container, SWT.NULL);
		label3.setText(Messages.InvoiceWizardPage_7);
		amountText = new Text(container, SWT.BORDER | SWT.SINGLE);
		amountText.setLayoutData(gd);
		amountText.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText(Messages.InvoiceWizardPage_8);
		conditionCombo = new Combo(container, SWT.READ_ONLY);
		conditionCombo.setLayoutData(gd);
		for (String condition : Invoice.CONDITIONS) {
			conditionCombo.add(condition);
		}
		conditionCombo.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText(Messages.InvoiceWizardPage_9);
		typeCombo = new Combo(container, SWT.READ_ONLY);
		typeCombo.setLayoutData(gd);
		for (String type : Invoice.TYPES) {
			typeCombo.add(type);
		}
		typeCombo.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label5 = new Label(container, SWT.NULL);
		label5.setText(Messages.InvoiceWizardPage_10);
		statusCombo = new Combo(container, SWT.READ_ONLY);
		statusCombo.setLayoutData(gd);
		for (String status : Invoice.STATUS) {
			statusCombo.add(status);
		}
		statusCombo.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label6 = new Label(container, SWT.NULL);
		label6.setText(Messages.InvoiceWizardPage_11);
		paymentDateText = new Text(container, SWT.BORDER | SWT.SINGLE);
		paymentDateText.setLayoutData(gd);
		paymentDateText.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
			
		setControl(container);
		initialize();
	}
	
	private void initialize() {
		if (invoice != null) {
			setDueDate(invoice.getDueDate());
			setAmount(invoice.getAmount());
			setCondition(invoice.getCondition());
			setType(invoice.getType());
			setStatus(invoice.getStatus());
			setPaymentDate(invoice.getPaymentDate());
			setPageComplete(true);
		} else {
			setPageComplete(false);
		}
	}
	
	private void dialogChanged() {
		boolean complete = true;
		if (getDueDate().length() == 0) {
			setDescription(Messages.InvoiceWizardPage_12);
			setErrorMessage(null);
			complete = false;
		}
		if (complete && getDueDate().length() != 0) {
			try {
				Converter.convertToDate(getDueDate());
				setErrorMessage(null);
			} catch (Exception e) {
				complete = false;
				setErrorMessage(Messages.InvoiceWizardPage_13);
			}
		} 
		if (complete && getAmount().length() == 0) {
			complete = false;
			setDescription(Messages.InvoiceWizardPage_14);
			setErrorMessage(null);
		}
		if (complete && getAmount().length() != 0) {
			try {
				Converter.convertToDouble(getAmount());
				setErrorMessage(null);
			} catch (Exception e) {
				complete = false;
				setErrorMessage(Messages.InvoiceWizardPage_15);
			}
		} 
		if (complete && getCondition().length() == 0) {
			complete = false;
			setDescription(Messages.InvoiceWizardPage_16);
			setErrorMessage(null);
		}
		if (complete && getType().length() == 0) {
			complete = false;
			setDescription(Messages.InvoiceWizardPage_17);
			setErrorMessage(null);
		} 
		if (complete && getStatus().length() == 0) {
			complete = false;
			setDescription(Messages.InvoiceWizardPage_18);
			setErrorMessage(null);
		}
		if (complete && getPaymentDate().length() != 0) {
			try {
				Converter.convertToDate(getPaymentDate());
				setErrorMessage(null);
			} catch (Exception e) {
				complete = false;
				setErrorMessage(Messages.InvoiceWizardPage_19);
			}
		}
		if (complete) {
			setDescription(null);
			setErrorMessage(null);
		}
		setPageComplete(complete);
	}
	
	public String getDueDate() {
		return dueDateText.getText().trim();
	}
	
	private void setDueDate(Date dueDate) {
		dueDateText.setText(Converter.convertToString(dueDate));
	}
	
	public String getAmount() {
		return amountText.getText().trim();
	}
	
	private void setAmount(double amount) {
		amountText.setText(String.valueOf(amount));
	}
	
	public String getCondition() {
		return conditionCombo.getText().trim();
	}
	
	private void setCondition(String condition) {
		conditionCombo.setText(condition);
	}
	
	public String getType() {
		return typeCombo.getText().trim();
	}
	
	private void setType(String type) {
		typeCombo.setText(type);
	}
	
	private void setStatus(String status) {
		statusCombo.setText(status);
	}
	
	public String getStatus() {
		return statusCombo.getText().trim();
	}
	
	public String getPaymentDate() {
		return paymentDateText.getText().trim();
	}
	
	private void setPaymentDate(Date paymentDate) {
		paymentDateText.setText(Converter.convertToString(paymentDate));
	}
	
	
	public Control getControl() {
		return container;
	}
}
